<?php

class DB {
	var $m_host = null;
	var $m_user = null;
	var $m_password = null;
	var $m_name = null;
	var $m_db;
	
	function __construct($host, $user, $password, $name) {
		$m_db = @mysql_connect($host, $user, $password);
		if (!$m_db) {
			throw new Exception('db-connect-error');
		}
		$db = @mysql_select_db($name);
		if (!$db) {
			throw new Exception('select-db-error');
		}
	}
	
	function disconnect() {
		mysql_close();
	}
	
	function get($str) {
		$res = $this->get_result($str);
		return $res[0];
	}
	
	function get_result($str) {
		$res = array();
		$qry = mysql_query($str) or kc_die(mysql_error());
		if (mysql_num_rows($qry) > 0) {
			while ($arr = mysql_fetch_array($qry)) {
				array_push($res, $arr);
			}
			return $res;
		}
		return null;
	}

	
	function execute($str) {
		$res = mysql_query($str) or kc_die(mysql_error());
		return $res;
	}
	
	function count($table, $field, $where = null) {
		$str = 'SELECT COUNT('.$field.') AS res FROM '.$table;
		if ($where) {
			$str .= ' WHERE '.$where;
		}
		$q = mysql_query($str) or kc_die(mysql_error());
		$arr = mysql_fetch_array($q);
		return (int) $arr['res'];
	}
	
	function sum() {
		
	}
}

class Session
{
	function __construct()
	{
		session_start();
	}
	
	function set($name, $value)
	{
		$_SESSION[$name] = $value;
	}
	
	function is_set($name)
	{
		return isset($_SESSION[$name]);
	}
	
	function get($name)
	{
		if (isset($_SESSION[$name]))
		{
			return $_SESSION[$name];
		}
		return null;
	}
	
	function del($name)
	{
		unset($_SESSION[$name]);
	}
}

?>
